import 'package:flutter/material.dart';

class KeepAliveDemo extends StatefulWidget {
  @override
  _KeepAliveDemo createState() => _KeepAliveDemo();
}

class _KeepAliveDemo extends State<KeepAliveDemo>
    with AutomaticKeepAliveClientMixin {
  int _conuter = 0;

  @override
  bool get wantKeepAlive => true;

  void _incrementCounter() {
    setState(() {
      _conuter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text("点一次增加一个数字"),
            Text(
              '$_conuter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: _incrementCounter,
        tooltip: "自增1",
      ),
    );
  }
}
